<?php

class HtmlHelper extends Helper {

    function image($path, $options = array()) {
        $img = '<img src="';
        $img .= Router::get_webroot() . 'images/' . $path . '" ';
        if (!empty($options)) {
            foreach($options as $key => $value) {
                $img .= $key . '="' . $value . '" ';
            }
        }
        $img .= ' />';
        return $img;
    }

    function image_url($path) {
        return Router::get_webroot() . 'images/' . $path;
    }

    function flash($path, $options = array()) {
        $options_default = array('quality'=>'high', 'wmode'=>'transparent', 'type'=>'application/x-shockwave-flash');
        $flash = '<embed src="';
        $flash .= Router::get_webroot() . 'flash/' . $path . '" ';
        foreach($options_default as $key => $value) {
            if (!isset($options[$key])) {
                $options[$key] = $value;
            }
        }

        if (!empty($options)) {
            foreach($options as $key => $value) {
                $flash .= $key . '="' . $value . '" ';
            }
        }
        $flash .= ' ></embed>';
        return $flash;
    }

    function input($name, $htmlAttributes = array()) {
        $input = '<input name="data[';
        $names = explode('/', $name);
        $input .= $names[0] . ']' . '['. $names[1] . ']" ';

        if (!isset($htmlAttributes['value'])) {
            if (isset($this->data[$names[0]][$names[1]]))
                $htmlAttributes['value'] = $this->data[$names[0]][$names[1]];
        }
        
        if (is_array($htmlAttributes)) {
            foreach($htmlAttributes as $key=>$value)
                $input .= $key . '="' . $value . '" ';
        }
        $input .= ' />';
        return $input;
    }

    function textarea($name, $htmlAttributes = array()) {
        $textarea_value = null;
        $textarea = '<textarea name="data[';
        $names = explode('/', $name);
        if (!isset($htmlAttributes['value'])) {
            if (isset($this->data[$names[0]][$names[1]]))
                $textarea_value = $this->data[$names[0]][$names[1]];
        } else {
            $textarea_value = $htmlAttributes['value'];
            unset($htmlAttributes['value']);
        }
        $textarea .= $names[0] . ']' . '['. $names[1] . ']" ';
        if (is_array($htmlAttributes)) {
            foreach($htmlAttributes as $key=>$value)
                $textarea .= $key . '="' . $value . '" ';
        }
        $textarea .= '>';
        $textarea .= $textarea_value;
        $textarea .= '</textarea>';
        return $textarea;
    }

    function admin_url($url) {
        $url = sprintf("/%s$url", ARLICLE_ADMIN);
        return $this->url($url);
    }

    function url($url) {
        return Router::get_url() . $url;
    }

    function www($path) {
        return Router::get_base() . 'www' . $path;
    }

    function link($title, $url = null, $htmlAttributes = array()) {
        if (empty($url) &&  empty($htmlAttributes)) {
            return $title;
        }
        if (!empty($url)) {
            if ($url == '/') {
                $url = Router::get_base();
            } else {
                $url = $this->url($url);
            }
            $link = '<a href="' . $url . '" ';
        } else {
            $link = '<a ';
        }
        if (is_array($htmlAttributes) && !empty($htmlAttributes)) {
            foreach($htmlAttributes as $key => $value) {
                $link .= $key . '="' . $value . '" ';
            }
        }
        $link .= '>' . $title . '</a>';
        return $link;
    }

    function admin_link($title, $url = null, $htmlAttributes = array()) {
        $url = sprintf("/%s$url", ARLICLE_ADMIN);
        return $this->link($title, $url, $htmlAttributes);
    }

    function js($path, $rel = null, $htmlAttributes = array()) {
        $path = Router::get_webroot() . 'js/' . $path;
        $js = '<script src="' . $path . '" type="text/javascript"></script>' . "\n";
        return $js;
    }

    function js_url($path) {
        return $path = Router::get_webroot() . 'js/' . $path;
    }

    function plugin_js($path, $rel = null, $htmlAttributes = array()) {
        $path = Router::get_plugin_webroot() . '/js/' . $path;
        $js = '<script src="' . $path . '" type="text/javascript"></script>' . "\n";
        return $js;
    }

    function css($path, $rel = null, $htmlAttributes = array()) {
        $path = Router::get_webroot() . 'css/' . $path;
        $css = '<link rel="stylesheet" type="text/css" href="' . $path . '" />' . "\n";
        return $css;
    }

    function css_url($path) {
        $path = Router::get_webroot() . 'css/' . $path;
        return $path;
    }

    function plugin_css($path, $rel = null, $htmlAttributes = array()) {
        $path = Router::get_plugin_webroot() . '/css/' . $path;
        $css = '<link rel="stylesheet" type="text/css" href="' . $path . '" />' . "\n";
        return $css;
    }

    function form($action, $method = 'POST', $options = array()) {
        $form = '<form method="' . $method . '" action="' . $action . '" ';
        if (is_array($options) && !empty($options)) {
            foreach ($options as $key => $value) {
                $form .= $key . '="' . $value . '" ';
            }
        }
        $form .= '>';
        return $form;
    }

    function radio($name, $htmlAttributes = array()) {
        $checked = null;
        $radio = '<input name="data[';
        $names = explode('/', $name);
        $htmlAttributes['type'] = 'radio';
        $radio .= $names[0] . ']' . '['. $names[1] . ']" ';
        if (isset($htmlAttributes['value']) && isset($this->data[$names[0]][$names[1]]) && $this->data[$names[0]][$names[1]] == $htmlAttributes['value']) {
            $checked = 'checked="checked"';
        }

        if (is_array($htmlAttributes)) {
            foreach($htmlAttributes as $key=>$value)
                $radio .= $key . '="' . $value . '" ';
        }
        $radio .= $checked;
        $radio .= ' />';
        return $radio;
    }

    function checkbox($name, $htmlAttributes = array()) {
        $checked = null;
        $checkbox = '<input name="data[';
        $names = explode('/', $name);
        $htmlAttributes['type'] = 'checkbox';
        $checkbox .= $names[0] . ']' . '['. $names[1] . ']" ';
        if (isset($htmlAttributes['value']) && isset($this->data[$names[0]][$names[1]]) && $this->data[$names[0]][$names[1]] == $htmlAttributes['value']) {
            $checked = 'checked="checked"';
        }

        if (is_array($htmlAttributes)) {
            foreach($htmlAttributes as $key=>$value)
                $checkbox .= $key . '="' . $value . '" ';
        }
        $checkbox .= $checked;
        $checkbox .= ' />';
        return $checkbox;
    }

    function select($name, $options, $htmlAttributes = array()) {
        $select = '<select name="data[';
        $names = explode('/', $name);
        $select .= $names[0] . ']' . '['. $names[1] . ']" ';

        if (!empty($htmlAttributes)) {
            foreach($htmlAttributes as $key => $value) {
                $select .= $key . '="' . $value . '" ';
            }
        }

        $select .= ' >';
        foreach($options as $key => $value) {
            $selected = null;
            if (isset($this->data[$names[0]][$names[1]]) && $this->data[$names[0]][$names[1]] == $key) {
                $selected = 'selected="selected"';
            }
            $select .= '<option value="' . $key . '" ' . $selected . ' >' . $value . '</option>';
        }
        $select .= '</select>';
        return $select;
    }

    function theme_path($path) {
        return  Router::get_webroot() . $path;
    }
}
?>
